package swd.maintain.project.sys_inc_table_maintain;

import com.pub.database.*;

public class Business {
    QueryBean gqbnDAO = null;
    public Business(QueryBean pqbnDAO) {
        gqbnDAO = pqbnDAO;
    }

    String exeInsSQL(View view) throws Exception {
        String lstrSQL = new String();
        try {
            String int_seqno = gqbnDAO.readSingleValueData(
                    "select nvl(max(seqno),0)+1 from  wkf_sys_table where dbf_no='" +
                    view.getins_dbf_no().trim() + "' ");
            lstrSQL =
                    "insert into wkf_sys_table(sys_no,seqno,dbf_no,backup_flag) ";
            lstrSQL += " values ('" + view.getsys_no() + "','" +
                    int_seqno +
                    "','" + view.getins_dbf_no().trim() + "','" + view.getins_backup_flag() +
                    "')";
            if (!gqbnDAO.executeSQL(lstrSQL)) {
                return "wkf_sys_table新增失敗";
            } else {
                lstrSQL = "insert into wkf_table(dbf_no,dbf_name,sys_no)";
                lstrSQL += " values ('" + view.getins_dbf_no() + "','" +
                        view.getins_dbf_name().trim() +
                        "','" + view.getsys_no().trim() + "')";
                if (!gqbnDAO.executeSQL(lstrSQL)) {
                    return "wkf_table新增失敗";
                }

            }

            return "";
        } catch (Exception e) {
            e.printStackTrace();
            throw new Exception(e);
        }
    }


    void exeDelSQL(View view, int pintSn) {
        String lstrSQL = new String();
        try {
            lstrSQL = "delete from  wkf_sys_table  where sys_no='" +
                      view.gets_no()[pintSn].trim() + "' and seqno='" +
                      view.getseqno()[pintSn].trim() + "'";
            gqbnDAO.executeSQL(lstrSQL);
            lstrSQL = "delete from wkf_table where dbf_no='" +
                      view.getdbf_no()[pintSn].trim() + "' and sys_no='" +
                      view.gets_no()[pintSn].trim() + "' ";
            gqbnDAO.executeSQL(lstrSQL);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    void setDataViewForm(DataViewForm DataViewForm, View view) throws Exception {

        //StringBuffer stbCondition = new StringBuffer();
        StringBuffer stbPageActionURI = new StringBuffer(
                "sys_inc_table_maintain.jsp?rowsn=");

        DataViewForm.setFieldsList(
                " '', sys.seqno,sys.dbf_no,t.dbf_name ,sys.backup_flag,sys.sys_no");
        DataViewForm.setTablesList(" wkf_sys_table sys,wkf_table t  ");
        if (view.getsys_no() != null && !view.getsys_no().trim().equals("")) {
            DataViewForm.setSQLCondition(
                    " sys.dbf_no=t.dbf_no and sys.sys_no='" + view.getsys_no() +
                    "' ");
        } else {
            DataViewForm.setSQLCondition("sys.dbf_no=t.dbf_no ");
        }
            DataViewForm.setOrderByFields("sys.seqno");
            DataViewForm.setOrderBySeq("asc");
        DataViewForm.setGroupBy(null);
        DataViewForm.setHaving(null);

        String strColRelation[][] = new String[6][];

        strColRelation[0] = new String[5];
        strColRelation[1] = new String[5];
        strColRelation[2] = new String[5];
        strColRelation[3] = new String[5];
        strColRelation[4] = new String[5];
        strColRelation[5] = new String[5];

        strColRelation[0][0] = "''";
        strColRelation[0][1] = "INPUT";
        strColRelation[0][2] = "WIDTH='10%' align='center'";
        strColRelation[0][3] = "<INPUT class='button b_normal' TYPE=\"BUTTON\" onclick=\"javascript:saveRowSn('%sn%');document.all.item('prgAction').value='delete';document.forms[0].submit();\" NAME=\"del\" VALUE=";
        strColRelation[0][4] = "'刪除'";

        strColRelation[1][0] = "'序號'";
        strColRelation[1][1] = "INPUT";
        strColRelation[1][2] = "WIDTH='30%'";
        strColRelation[1][3] = "<INPUT TYPE='text' size='8' name='seqno' readonly CLASS='input_noborder' onchange=\"javascript:saveRowSn('%sn%');\" VALUE=";
        strColRelation[1][4] = "1";

        strColRelation[2][0] = "'檔案編號'";
        strColRelation[2][1] = "INPUT";
        strColRelation[2][2] = "WIDTH='30%'";
        strColRelation[2][3] = "<INPUT TYPE='text' size='8' name='dbf_no' readonly CLASS='input_noborder' onchange=\"javascript:saveRowSn('%sn%');\" VALUE=";
        strColRelation[2][4] = "2";

        strColRelation[3][0] = "'檔案名稱'";
        strColRelation[3][1] = "INPUT";
        strColRelation[3][2] = "WIDTH='30%'";
        strColRelation[3][3] = "<INPUT TYPE='text' size='8' name='dbf_name' readonly CLASS='input_noborder' onchange=\"javascript:saveRowSn('%sn%');\" VALUE=";
        strColRelation[3][4] = "3";

        strColRelation[4][0] = "'備份是否'";
        strColRelation[4][1] = "INPUT";
        strColRelation[4][2] = "WIDTH='30%'";
        strColRelation[4][3] = "<INPUT TYPE='text' size='8' name='backup_flag' readonly CLASS='input_noborder' onchange=\"javascript:saveRowSn('%sn%');\" VALUE=";
        strColRelation[4][4] = "4";

        strColRelation[5][0] = "'系統編號'";
        strColRelation[5][1] = "INPUT";
        strColRelation[5][2] = "WIDTH='30%'";
        strColRelation[5][3] = "<INPUT TYPE='text' size='8' name='s_no' readonly CLASS='input_noborder' onchange=\"javascript:saveRowSn('%sn%');\" VALUE=";
        strColRelation[5][4] = "5";

        DataViewForm.setColRelation(strColRelation); //將欄位相關設定殖入ActionForm
        DataViewForm.setJumpPageButtonShow(true); //是否要秀出上下頁按鈕
        DataViewForm.setPageBtnOnTop(false); //將button and rows info 秀在頂端
        DataViewForm.setPageBtnOnButtom(true); //將button and rows info 秀在底端
        DataViewForm.setFilterShow(false); //是否要秀出過濾選擇欄位
        DataViewForm.setJumpPageShow(true); //是否要秀出跳頁下拉式選單
        DataViewForm.setRowsInfoShow(false); //是否要秀出筆數資訊
        DataViewForm.setRowPageShow(false); //是否要秀出每頁筆數控制欄
        DataViewForm.setExcelShow(false); //是否要秀出轉Excel button
        DataViewForm.setOrderEnable(false); //是否要提供排序點選功能
        DataViewForm.setShowHeaderWhenNoData(false); //當ResultSet無資料是否要秀Header

        DataViewForm.setTableHeaderHtml(
                "class='td_head_grid' align='center'  width='680px'");
        //設定上一頁與下一頁按鈕的form action(通常為母頁即本頁)
        DataViewForm.setPageActionURI(stbPageActionURI.toString());
    }


    String checkNotNullFld(View view) throws Exception {
        StringBuffer lstbNullFldList = new StringBuffer();
        lstbNullFldList.append(view.getins_dbf_no() == null ?
                               "資料庫代碼不得為空值!" + "," : "");
        lstbNullFldList.append(view.getsys_no() == null ? "系統編號不得為空值!" + "," :
                               "");

        return (lstbNullFldList.toString().equals("") ? "" :
                lstbNullFldList.
                delete(lstbNullFldList.length() - 1, lstbNullFldList.length()).
                toString());
    }

    void setInitValueForAdd(View view, String pstrUserID) throws Exception {
    }

    void setReadonlyForAdd(View view) throws Exception {

    }

}
